/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.inner;

import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.service.FileStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.annotation.Resource;

@RestController
@RequestMapping("/inner/file")
@Api(produces = "application/json", tags = "文件存储接口" )
public class FileController extends ResourceHttpRequestHandler {

	@Resource
	private FileStoreService fileStoreService;

	@ApiOperation(httpMethod = "POST", value = "上传文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "bucket", value = "文件库名", paramType = "path", dataTypeClass = String.class),
			@ApiImplicitParam(name = "key", value = "文件KEY", paramType = "path", dataTypeClass = String.class)
	})
	@PostMapping(value = "/{bucket}/{key:.+}")
	public ResponseEntity<Void> upload(@RequestPart("file") MultipartFile file, @PathVariable String bucket, @PathVariable String key) {
		fileStoreService.upload(file, bucket, key);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "DELETE", value = "删除文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "bucket", value = "文件库名", paramType = "path", dataTypeClass = String.class),
			@ApiImplicitParam(name = "key", value = "文件KEY", paramType = "path", dataTypeClass = String.class)
	})
	@DeleteMapping(value = "/{bucket}/{key:.+}")
	public ResponseEntity<Void> delete(@PathVariable String bucket, @PathVariable String key) {
		fileStoreService.delete(bucket, key);
		return ResponseEntity.ok();
	}

}
